package com.sskj.wallet.ui.activity;


import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.LazyLoaders;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.UpdatePwdActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.WALLET_UPDATE_PWD)//修改密码
public class UpdatePwdActivity extends BaseActivity<UpdatePwdActivityPresenter> {
    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private boolean isSeeAgain;
    private boolean isSee;
    @Autowired
    String code;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;
    private WalletBean walletBean;
    private AndroidNextInputs inputs;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_update_pwd;
    }

    @Override
    public UpdatePwdActivityPresenter getPresenter() {
        return new UpdatePwdActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.wallet_updatePwdActivity1_j));

        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            isSeeAgain = !isSeeAgain;
            etPwdAgain.setTransformationMethod(isSeeAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggleAgain.setImageResource(isSeeAgain ? R.mipmap.lib_icon_show : R.mipmap.lib_icon_hide);
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.lib_icon_show : R.mipmap.lib_icon_hide);

        });
        inputs = new AndroidNextInputs();
        WidgetAccess widgetAccess = new WidgetAccess(this);
        LazyLoaders lazyLoaders = new LazyLoaders(this);
        inputs.clear();
        inputs
                .add(widgetAccess.findEditText(R.id.etPwd), StaticScheme.required(), SchemeUtil.getPwdScheme())
                .add(widgetAccess.findEditText(R.id.etPwdAgain), ValueScheme.equalsTo(lazyLoaders.fromEditText(R.id.etPwd)))
        ;
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                walletBean.setPwd(etPwd.getText().toString());
                walletBeanViewModel.insert(walletBean);
                ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_updatePwdActivity2_j));
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }
                if (code.equals(CoinEnum.BTC.getCode())) {
                    walletBean = walletBeanBtc;
                } else {
                    walletBean = walletBeanEth;
                }
            }
        });
    }


}
